HAL WWDG initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 WWDG configuration panel.

  • Configure the SW layer HAL.

  • Configure the WWDG main features.

  • Configure the NVIC if the application requires the WWDG to be used in IT mode.

  • Generate the code.

HAL WWDG configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

HAL WWDG main features configuration

Configure the WWDG main features.

WWDG main feature configuration

NVIC configuration

Configure the NVIC in case of an interrupt.

WWDG NVIC configuration

Generated WWDG initialization and de-initialization sequences

The WWDG initialization and de-initialization sequences are generated in the mx_wwdg_init function in the mx_wwdg.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

STM32CubeMX2 Generated : WWDG initialization sequence

Topic

HAL 1 Code Snippet

HAL 2 Code Snippet

Declaration of the handle

WWDG_HandleTypeDef hwwdg;
static hal_wwdg_handle_t hWWDG;

initialization and configuration sequence function.

static void MX_WWDG_Init(void)
{

  /* USER CODE BEGIN WWDG_Init 0 */

  /* USER CODE END WWDG_Init 0 */

  /* USER CODE BEGIN WWDG_Init 1 */

  /* USER CODE END WWDG_Init 1 */
  hwwdg.Instance = WWDG;
  hwwdg.Init.Prescaler = WWDG_PRESCALER_1;
  hwwdg.Init.Window = 64;
  hwwdg.Init.Counter = 64;
  hwwdg.Init.EWIMode = WWDG_EWI_DISABLE;
  if (HAL_WWDG_Init(&hwwdg) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN WWDG_Init 2 */

  /* USER CODE END WWDG_Init 2 */

}
hal_wwdg_handle_t *mx_wwdg_init(void)
{
  if (HAL_WWDG_Init(&hWWDG, HAL_WWDG1) != HAL_OK)
  {
    return NULL;
  }

  HAL_RCC_WWDG_EnableClock();

  /* Enable the interruption for WWDG */
  HAL_CORTEX_NVIC_SetPriority(WWDG_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(WWDG_IRQn);

  return &hWWDG;
}

hal_status_t mx_wwdg_start(void)
{
  return HAL_WWDG_Start(&hWWDG, 0UL, 64UL, 1UL);
}